﻿#include "mbaseview.h"
#include "line_item.h"
#include <QDebug>

#include <QGraphicsLineItem>
#include <mbasescene.h>
#include <QtMath>



MBaseView::MBaseView(QGraphicsScene *scene , QWidget *parent)
    : QGraphicsView(parent)
{
    this->setScene(scene);
    this->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing
                         | QPainter::HighQualityAntialiasing | QPainter::LosslessImageRendering);
    this->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
    this->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
    //关闭视图的移动框
    //    this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    //    this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    //消除复选框,就是鼠标长按拖拽的一个效果
    this->setDragMode(QGraphicsView::NoDrag);
    //默认将事件传播至最顶层的图元，可以小小测试一下
    this->setContextMenuPolicy(Qt::CustomContextMenu);
    //实时响应鼠标移动事件，不加这一行会导致只有鼠标点击的时候才会有这样的操作
    this->setMouseTracking(true);

}

MBaseView::~MBaseView()
{

}


void MBaseView::wheelEvent(QWheelEvent *event)
{
    event->delta() > 0 ? scale(1.05,1.05) : scale(0.95,0.95);
}

void MBaseView::mousePressEvent(QMouseEvent *event)
{
    LocatePoint = mapToScene(event->pos());
    QGraphicsView::mousePressEvent(event);
}

void MBaseView::mouseMoveEvent(QMouseEvent *event)
{
    mousePt = mapToScene(event->pos());
    viewport()->update();
    QGraphicsView::mouseMoveEvent(event);
}

void MBaseView::mouseReleaseEvent(QMouseEvent *event)
{
    QGraphicsView::mouseReleaseEvent(event);
}




void MBaseView::drawForeground(QPainter *painter, const QRectF &rect)
{
    if (MBaseScene::isbedrawLine) {
        QPen pen(Qt::red, 1 , Qt::SolidLine);
        painter->setPen(pen);
        painter->drawLine(mousePt.x() - 30, mousePt.y(), mousePt.x() + 30, mousePt.y());
        painter->drawLine(mousePt.x(), mousePt.y() - 30, mousePt.x(), mousePt.y() + 30);
    }
    QGraphicsView::drawForeground(painter,rect);
    viewport()->update();//这里去刷新viewport，退出时能够及时的
}
